// Copyright 2006 PR1ME. All Rights Reserved.
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;

/**
 * Represents a single file indexed by the corpus; the actual referenced file
 * may or may not exist.
 */
public class CorpusFile {

  public CorpusFile(String filePath, BitSet bitSet) {
    this.filePath = filePath;
    this.bitSet = bitSet;
  }

  public BitSet getBitSet() {
    return bitSet;
  }

  public String getContents() {
    try {
      InputStream in = FileStreamCreator.createInputStream(filePath);
      StringBuffer text = new StringBuffer();

      // Read the input stream in chunks to avoid tons of slow IO calls
      int readCount;
      final int BUF_SIZE = 8192;
      byte[] buf = new byte[BUF_SIZE];
      while ((readCount = in.read(buf, 0, BUF_SIZE)) >= 0) {
        // Process all the bytes we've read so far
        for (int i = 0; i < readCount; ++i) {
          // force to positive int in range 0 to 255
          int ch = buf[i] & 0xFF;
          text.append((char) ch);
        }
      }

      return text.toString();
    } catch (FileNotFoundException e) {
      Util.errExit("Corpus file '" + filePath + "' does not exist");
    } catch (IOException e) {
      Util.errExit("Corpus file '" + filePath + "' could not be read from");
    }
    return null;
  }

  public String getFilePath() {
    return filePath;
  }

  public String toString() {
    return filePath;
  }

  private final BitSet bitSet;
  private final String filePath;

}
